package cs342.uwosh.edu.MyWorldApp.storage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import android.os.Parcel;
import android.os.Parcelable;

public class User implements Parcelable {
	
	public static final int PARCEL_DESCRIPTION = 0;
	
	private String userName;
	private boolean stealthed;
	private boolean searchingPlaces;
	private Location location;
	public static ArrayList<Friend> friendsList;
	private boolean locked;
	
	public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
		public User createFromParcel(Parcel in) {     
			return new User(in);
		}
		
		public User[] newArray(int size) {
		    return new User[size];
		}
	};
			
	public User(Parcel in) {
		userName = in.readString();
		location = in.readParcelable(Location.class.getClassLoader());
		friendsList = new ArrayList<Friend>();
		in.readList(friendsList, Friend.class.getClassLoader());
		
		if(in.readInt() == 1) {
			stealthed = true;
		} else {
			stealthed = false;
		}
		if(in.readInt() == 1) {
			searchingPlaces = true;
		} else {
			searchingPlaces = false;
		}
		locked = false;
	}
	
	public User(String userName, Location location, boolean stealthed) {
		this.userName = userName;
		this.location = location;
		this.stealthed = stealthed;
		this.searchingPlaces = false;
		this.friendsList = new ArrayList<Friend>();
		this.locked = false;
	}
	
	public Location getLocationOf(Friend friend) {
		int index = Collections.binarySearch(friendsList, friend);
		return friendsList.get(index).getLocation();
	}
	
	public int describeContents() {
		return PARCEL_DESCRIPTION;
	}

	public void writeToParcel(Parcel out, int arg1) {
		// TODO Auto-generated method stub
		out.writeString(userName);
		out.writeParcelable(location, 0);
		out.writeList(friendsList);
		if(stealthed) {
			out.writeInt(1);
		} else {
			out.writeInt(0);
		}
		if(searchingPlaces) {
			out.writeInt(1);
		} else {
			out.writeInt(0);
		}
	}

	public String getUserName() {
		return userName;
	}
	
	
	public void addFriend(Friend friend) {
		//simple sorted insert
		int i = 0;
		while( i < friendsList.size() 
				&& friend.compareTo(friendsList.get(i)) == -1) i++;
		friendsList.add(i, friend);
	}
	
	public void updateFriend(Friend friend) {
		int index = Collections.binarySearch(friendsList, friend);
		friendsList.remove(index);
		friendsList.add(index, friend);
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public ArrayList<Friend> getFriendsList() {
		return friendsList;
	}
	
	public String[][] getFriendsLocations() {
		String[][] locations = new String[friendsList.size()][3];
		for(int i = 0; i < locations.length; i ++) {
			locations[0] = friendsList.get(i).getLocation().toStringArray();
			locations[0][2] = friendsList.get(i).getUserName();
		}
		return locations;
	}
	
	public void setLocation(Location location) {
		this.location = location;
	}

	public boolean isStealthed() {
		return stealthed;
	}

	public void setStealthed(boolean stealthed) {
		this.stealthed = stealthed;
	}
	
	public void lock() {
		locked = true;
	}
	
	public void unlock() {
		locked = false;
	}
	
	public boolean isLocked() {
		return locked;
	}

	public boolean isSearchingPlaces() {
		return searchingPlaces;
	}

	public void setSearchingPlaces(boolean searchingPlaces) {
		this.searchingPlaces = searchingPlaces;
	}
	
	public void setSatelliteMode()
	{
		
	}

	
}


